<template>
	<view class="content">
		<u-search class="search_box" placeholder="请输入内容" :clearabled="true" :show-action="false" @search="search"></u-search>
		<view class="wrap"><u-swiper :list="adsList" name="image"></u-swiper></view>
		<u-grid :col="3" @click="chooseType">
			<u-grid-item :index="1">
				<u-icon name="gift-fill" :size="46" color="#ff9900"></u-icon>
				<view class="grid-text">礼品</view>
			</u-grid-item>
			<u-grid-item :index="2">
				<u-icon name="camera-fill" :size="46" color="#a0cfff"></u-icon>
				<view class="grid-text">数码</view>
			</u-grid-item>
			<u-grid-item :index="3">
				<u-icon name="thumb-up-fill" :size="46" color="#fa3534"></u-icon>
				<view class="grid-text">热销</view>
			</u-grid-item>
			<u-grid-item :index="4">
				<u-icon name="heart-fill" :size="46" color="#fa3534"></u-icon>
				<view class="grid-text">猜你喜欢</view>
			</u-grid-item>
			<u-grid-item :index="5">
				<u-icon name="file-text-fill" :size="46" color="#19be6b"></u-icon>
				<view class="grid-text">图书</view>
			</u-grid-item>
			<u-grid-item :index="6">
				<u-icon name="integral-fill" :size="46" color="#2979ff"></u-icon>
				<view class="grid-text">奢侈品</view>
			</u-grid-item>
		</u-grid>
		<view class="wrap">
			<u-waterfall v-model="flowList" ref="uWaterfall">
				<template v-slot:left="{ leftList }">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index">
						<!-- 警告：微信小程序不支持嵌入lazyload组件，请自行如下使用image标签 -->
						<!-- #ifndef MP-WEIXIN -->
						<u-lazy-load threshold="-450" border-radius="10" :image="item.image" :index="index"></u-lazy-load>
						<!-- #endif -->
						<!-- #ifdef MP-WEIXIN -->
						<view class="demo-img-wrap"><image class="demo-image" :src="item.image" mode="widthFix"></image></view>
						<!-- #endif -->
						<view class="demo-title">{{ item.title }}</view>
						<view class="demo-price">{{ item.price }}元</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">自营</view>
							<view class="demo-tag-text">放心购</view>
						</view>
						<view class="demo-shop">{{ item.shop }}</view>
					</view>
				</template>
				<template v-slot:right="{ rightList }">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index">
						<!-- #ifndef MP-WEIXIN -->
						<u-lazy-load threshold="-450" border-radius="10" :image="item.image" :index="index"></u-lazy-load>
						<!-- #endif -->
						<!-- #ifdef MP-WEIXIN -->
						<view class="demo-img-wrap"><image class="demo-image" :src="item.image" mode="widthFix"></image></view>
						<!-- #endif -->
						<view class="demo-title">{{ item.title }}</view>
						<view class="demo-price">{{ item.price }}元</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">自营</view>
							<view class="demo-tag-text">放心购</view>
						</view>
						<view class="demo-shop">{{ item.shop }}</view>
					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
		</view>
		<u-back-top :scroll-top="scrollTop" tips="顶部" icon="arrow-up" :custom-style="customStyle" :icon-style="iconStyle"></u-back-top>
	</view>
</template>

<script>
export default {
	data() {
		return {
			scrollTop: 0,
			customStyle: {
				textAlign: 'center',
				background: 'rgba(0,0,0, 0.4)',
				color: '#fff',
			},
			iconStyle: {
				color: '#fff',
			},
			adsList: [],
			loadStatus: 'loadmore',
			flowList: [],
			list: [
				{
					price: 35,
					title: '北国风光，千里冰封，万里雪飘',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23327_s.jpg'
				},
				{
					price: 75,
					title: '望长城内外，惟余莽莽',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23325_s.jpg'
				},
				{
					price: 385,
					title: '大河上下，顿失滔滔',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2119_s.jpg'
				},
				{
					price: 784,
					title: '欲与天公试比高',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/zzpic23369_s.jpg'
				},
				{
					price: 7891,
					title: '须晴日，看红装素裹，分外妖娆',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2130_s.jpg'
				},
				{
					price: 2341,
					shop: '李白杜甫白居易旗舰店',
					title: '江山如此多娇，引无数英雄竞折腰',
					image: 'http://pic1.sc.chinaz.com/Files/pic/pic9/202002/zzpic23346_s.jpg'
				},
				{
					price: 661,
					shop: '李白杜甫白居易旗舰店',
					title: '惜秦皇汉武，略输文采',
					image: 'http://pic1.sc.chinaz.com/Files/pic/pic9/202002/zzpic23344_s.jpg'
				},
				{
					price: 1654,
					title: '唐宗宋祖，稍逊风骚',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic1.sc.chinaz.com/Files/pic/pic9/202002/zzpic23343_s.jpg'
				},
				{
					price: 1678,
					title: '一代天骄，成吉思汗',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic1.sc.chinaz.com/Files/pic/pic9/202002/zzpic23343_s.jpg'
				},
				{
					price: 924,
					title: '只识弯弓射大雕',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic1.sc.chinaz.com/Files/pic/pic9/202002/zzpic23343_s.jpg'
				},
				{
					price: 8243,
					title: '俱往矣，数风流人物，还看今朝',
					shop: '李白杜甫白居易旗舰店',
					image: 'http://pic1.sc.chinaz.com/Files/pic/pic9/202002/zzpic23343_s.jpg'
				}
			]
		};
	},
	onLoad() {
		this.$http.get('rest/ads').then(res => {
			this.adsList = res.data;
		});
		this.addRandomData();
	},
	onReachBottom() {
		this.loadStatus = 'loading';
		// 模拟数据加载
		setTimeout(() => {
			this.addRandomData();
			this.loadStatus = 'loadmore';
		}, 1000);
	},
	methods: {
		search(val) {
			// 搜索商品
			console.log(val);
		},
		chooseType(index) {
			// 点击九宫格图标
			console.log(index);
		},
		addRandomData() {
			for (let i = 0; i < 10; i++) {
				let index = this.$u.random(0, this.list.length - 1);
				// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
				let item = JSON.parse(JSON.stringify(this.list[index]));
				item.id = this.$u.guid();
				this.flowList.push(item);
			}
		},
		remove(id) {
			this.$refs.uWaterfall.remove(id);
		},
		clear() {
			this.$refs.uWaterfall.clear();
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		}
	}
};
</script>

<style lang="scss">
.content {
	.search_box {
		padding: 20upx 0;
	}
	.wrap {
		margin: 20upx 0;
	}
	.grid-text {
		font-size: 16upx;
	}

	.demo-warter {
		border-radius: 8upx;
		margin: 10upx;
		background-color: rgb(240, 240, 240);
		padding: 8upx;
		position: relative;
		// border: 1upx solid #eeeeee;
	}

	.u-close {
		position: absolute;
		top: 32upx;
		right: 32upx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4upx;
	}

	.demo-title {
		font-size: 30upx;
		margin-top: 5upx;
		color: $u-main-color;
	}

	.demo-tag {
		display: flex;
		margin-top: 5upx;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #ffffff;
		display: flex;
		align-items: center;
		padding: 4upx 14upx;
		border-radius: 50upx;
		font-size: 20upx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1upx solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10upx;
		border-radius: 50upx;
		line-height: 1;
		padding: 4upx 14upx;
		display: flex;
		align-items: center;
		border-radius: 50upx;
		font-size: 20upx;
	}

	.demo-price {
		font-size: 30upx;
		color: $u-type-error;
		margin-top: 5upx;
	}

	.demo-shop {
		font-size: 22upx;
		color: $u-tips-color;
		margin-top: 5upx;
	}
}
</style>
